package com.mrlee.controller;

import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Role;
import com.mrlee.bean.Service;
import com.mrlee.mapper.ServiceUpdateMapper;
import com.mrlee.service.ServiceService;
import com.mrlee.utils.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/3/15.
 */
@Controller
public class ServiceController {

    @Resource
    private ServiceService serviceService;

    @Resource
    private ServiceUpdateMapper serviceUpdateMapper;

    /**
     * 业务账号列表页
     * @return
     */
    @RequestMapping(value = "servicelist")
    public String serviceList(){
        return "/service/service_list";
    }

    /**
     * 业务账号添加
     * @return
     */
    @RequestMapping(value = "serviceadd")
    public String serviceAdd(){
        return "/service/service_add";
    }

    /**
     * 业务账号详情页
     * @return
     */
    @RequestMapping(value = "servicedetail")
    public String serviceDetail(){
        return "/service/service_detail";
    }

    /**
     * 业务账号修改页
     * @return
     */
    @RequestMapping(value = "servicemodi")
    public String serviceModi(){
        return "/service/service_modi";
    }


    /**
     * 查询全部业务账号
     * @return
     */
    @ResponseBody
    @RequestMapping("allservice")
    public AjaxResult allService(){
        return serviceService.selectAllService();
    }

    /**
     * 分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "servicepage")
    @ResponseBody
    public PageInfo<Service> roleByPage(@RequestParam(value = "pagenum",required = false)Integer pageNo,
                                     @RequestParam(value = "pagesize",required = false)Integer pageSize){
        return serviceService.queryServiceByPage(pageNo,3);
    }

    /**
     * 修改状态
     * @param serviceId 业务Id
     * @param status 状态
     * @param accountId 账务账号Id
     * @return
     */
    @ResponseBody
    @RequestMapping("changeservicestatus")
    public AjaxResult changeServiceStatus(@RequestParam("serviceId")Integer serviceId, @RequestParam("status")Integer status,@RequestParam("accountId")Integer accountId){
        return serviceService.changeStatus(serviceId,status,accountId);
    }

    /**
     * 添加业务
     * @param accountId 账务账号Id
     * @param unixHost ip地址
     * @param osusername 业务账号
     * @param password 密码
     * @param password1 确认密码
     * @param costId 资费Id
     * @return
     */
    @ResponseBody
    @RequestMapping("addservice")
    public AjaxResult addService(@RequestParam("accountId")Integer accountId,
                                 @RequestParam("unixHost")String unixHost,
                                 @RequestParam("osusername")String osusername,
                                 @RequestParam("password")String password,
                                 @RequestParam("password1")String password1,
                                 @RequestParam("costId")Integer costId){
        AjaxResult ajaxResult = new AjaxResult();
        if (!password.equals(password1)){
            ajaxResult.setCode("0");
            ajaxResult.setMessage("密码不一致");
            return ajaxResult;
        }
        Service service = new Service();
        service.setAccountId(accountId);
        service.setUnixHost(unixHost);
        service.setOsUsername(osusername);
        service.setLoginPasswd(password);
        service.setCostId(costId);

        return serviceService.addService(service);
    }

    /**
     * 根据业务Id查找业务信息
     * @param serviceId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectservice")
    public AjaxResult selectService(@RequestParam("serviceId")Integer serviceId){
        return serviceService.selectService(serviceId);
    }

    /**
     * 修改资费套餐
     * @param serviceId 业务Id
     * @param costId 资费Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updatecost")
    public AjaxResult updateCost(@RequestParam("serviceId")Integer serviceId,@RequestParam("costId")Integer costId){
        AjaxResult result = new AjaxResult();
        int num = serviceUpdateMapper.addUpdate(serviceId,costId);
        if (num == 0){
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("修改成功下月生效");
        return result;
    }

    /**
     * 动态搜索业务
     * @param osname 业务账号
     * @param cardid 身份证号
     * @param status 状态
     * @param ip ip
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "searchservice")
    public AjaxResult search(@RequestParam("osname") String osname,
                             @RequestParam("cardid") String cardid,
                             @RequestParam("status") String status,
                             @RequestParam("ip") String ip){
        return serviceService.dynamicSearch(osname,cardid,status,ip);
    }
}
